<style>
.app-main{
  background: #f5f9ff;
}
</style>
<template>
  <div class="app-container" style="margin: auto; margin-top: 20px; border-radius:5px; width: 80%; background: #FFFFFF">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-button plain>导入</el-button>
      <el-button @click="handleExport" plain>导出</el-button>
      <el-button plain @click="dayin">打印</el-button><br>
      <br>
      <el-form-item label="始发地" prop="departureLocation" >
        <el-select v-model="queryParams.departureLocation" clearable placeholder="请选择">
          <el-option label="北京" value="北京"></el-option>
          <el-option label="广州" value="广州"></el-option>
          <el-option label="杭州" value="杭州"></el-option>
          <el-option label="成都" value="成都"></el-option>
          <el-option label="武汉" value="武汉"></el-option>
          <el-option label="西安" value="西安"></el-option>
          <el-option label="沈阳" value="沈阳"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="destination" >
        <el-select v-model="queryParams.destination"  clearable placeholder="请选择">
          <el-option label="上海" value="上海"></el-option>
          <el-option label="深圳" value="深圳"></el-option>
          <el-option label="南京" value="南京"></el-option>
          <el-option label="重庆" value="重庆"></el-option>
          <el-option label="长沙" value="长沙"></el-option>
          <el-option label="郑州" value="郑州"></el-option>
          <el-option label="大连" value="大连"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户" prop="customer" >
        <el-input

          v-model="queryParams.customer"
          placeholder="请输入客户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签收时间">
        <el-date-picker
          v-model="daterangeCreatedTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="receiptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderNumber" width="150px" >
        <template slot-scope="scope">
          <el-link @click="handleUpdate(scope.row)" type="primary">{{scope.row.orderNumber}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customer" width="150px"/>
      <el-table-column label="始发地" align="center" prop="departureLocation" width="150px" />
      <el-table-column label="目的地" align="center" prop="destination" width="150px"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus" width="150px"/>
      <el-table-column label="发货地址" align="center" prop="shippingAddress" width="150px"/>
      <el-table-column label="收货地址" align="center" prop="receivingAddress" width="150px"/>
      <el-table-column label="订单数量" align="center" prop="orderQuantity" width="150px"/>
      <el-table-column label="签收数量" align="center" prop="signedQuantity" width="150px"/>
      <el-table-column label="签收人" align="center" prop="signedBy" width="150px"/>
      <el-table-column label="签收时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operation:operation:edit']"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改签收单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="客户" prop="customer">
          <el-input v-model="form.customer" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="发货地址" prop="shippingAddress">
          <el-input v-model="form.shippingAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收货地址" prop="receivingAddress">
          <el-input v-model="form.receivingAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单数量" prop="orderQuantity">
          <el-input v-model="form.orderQuantity" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="签收数量" prop="signedQuantity">
          <el-input v-model="form.signedQuantity" placeholder="请输入签收数量" />
        </el-form-item>
        <el-form-item label="签收人" prop="signedBy">
          <el-input v-model="form.signedBy" placeholder="请输入签收人" />
        </el-form-item>
        <el-form-item label="签收时间" prop="createdTime">
          <el-date-picker clearable
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签收时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceipt, getReceipt, delReceipt, addReceipt, updateReceipt } from "@/api/receipt/receipt";

export default {
  name: "Receipt",
  data() {
    return {

      daterangeCreatedTime:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 签收单表格数据
      receiptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customer: null,
        departureLocation: null,
        destination: null,
        orderStatus: null,
        createdTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        customer: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        departureLocation: [
          { required: true, message: "始发地不能为空", trigger: "change" }
        ],
        destination: [
          { required: true, message: "目的地不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        shippingAddress: [
          { required: true, message: "发货地址不能为空", trigger: "blur" }
        ],
        receivingAddress: [
          { required: true, message: "收货地址不能为空", trigger: "blur" }
        ],
        orderQuantity: [
          { required: true, message: "订单数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询签收单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreatedTime && '' != this.daterangeCreatedTime) {
        this.queryParams.params["beginCreatedTime"] = this.daterangeCreatedTime[0];
        this.queryParams.params["endCreatedTime"] = this.daterangeCreatedTime[1];
      }
      listReceipt(this.queryParams).then(response => {
        this.receiptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        customer: null,
        departureLocation: null,
        destination: null,
        orderStatus: null,
        shippingAddress: null,
        receivingAddress: null,
        orderQuantity: null,
        signedQuantity: null,
        signedBy: null,
        createdTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    dayin(){
      window.print();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加签收单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      this.$router.push({path:'/pickup/pickupXq',query:{id:id}})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceipt(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceipt(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除签收单编号为"' + ids + '"的数据项？').then(function() {
        return delReceipt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('receipt/receipt/export', {
        ...this.queryParams
      }, `receipt_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
